<template>
  <div class="todo-footer">
    <label>
      <input type="checkbox" />
    </label>
    <span>
      <span>已完成{{finishTodo}}</span> / 全部{{total}}
    </span>
    <button class="btn btn-danger" @click="handleClearAllDone">清除已完成任务</button>
  </div>
</template>

<script>
export default {
  name: 'Footer',
  props:['todos','clearCheckedTodo'],
  computed: {
    // 计算总数
    total(){
      return this.todos.length
    },
    // 计算已完成的数量
    finishTodo(){
      // 传统的一个写法
      /* let n = 0
      this.todos.forEach(item => {
        if(item.done){
          n+=1
        }
      });
      return n */

      // 用filter也能实现
      return this.todos.filter(item =>item.done).length
    },
  },
  methods: {
    // 点击【清除已完成任务】的回调
    handleClearAllDone(){
      if(confirm('确定删除所有已完成的吗？')){
        // 联系App去把所有done值为true的删掉
        this.clearCheckedTodo()
      }
    }
  },
}
</script>

<style scoped>
/*footer*/
.todo-footer {
  height: 40px;
  line-height: 40px;
  padding-left: 6px;
  margin-top: 5px;
}
.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}
.todo-footer label input {
  position: relative;
  top: -1px;
  vertical-align: middle;
  margin-right: 5px;
}
.todo-footer button {
  float: right;
  margin-top: 5px;
}
</style>